package club.justwill.single.persistence.jpo;

import club.justwill.single.persistence.support.ProjectsBase;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by luoyu on 15-12-24.
 */
@Entity
@Table(name = "projects")
@NamedQueries({
        @NamedQuery(name="Project.search",
                query="select pro from Projects pro order by pro.processTimeStamp desc"),
        @NamedQuery(name="Project.getByProjectId",
                query="select pj from Projects pj where pj.projectId = :projectId"),
        @NamedQuery(name="Project.searchProjectsByModelId",
                query="select pj from Projects pj where pj.modelId = :modelId order by pj.processTimeStamp"),
        @NamedQuery(name="Project.searchProjectsByIdList",
                query="select pj from Projects pj where pj.projectId in (:followedProjectIdList)"),
        @NamedQuery(name="Project.getFinishedProject",
                query="select pro from Projects pro where pro.status = 20 and pro.type=0")
})
public class Projects extends ProjectsBase {

    @Id
    @Column(name = "project_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long projectId ;

    //一个用户可以创建多个项目，项目跟用户是多对一 多方
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id", insertable = false, updatable = false)
    private User user;

    //一个项目签订一个协议 项目跟协议是一对一
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "projects")
    private Set<Agreement> agreement;

    //一个项目有多个介绍，项目跟介绍是一对多关系 一方
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "projects")
    @OrderBy("tum")
    private Set<ProjectIntroduce> projectIntroduceSet;
    //一个项目对应多个sku 项目跟sku是一对多关系 一方

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "projects")
    @OrderBy("unitprice")
    private Set<Skus> skusSet;

    //一个项目可以有多次收入 项目跟收入是一对多关系 一方
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "projects")
    @OrderBy("proc_ts desc")
    private Set<ProjectIncome> projectIncomeSet;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "model_id", referencedColumnName = "model_id", insertable = false, updatable = false)
    private Model model;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "slaveProject")
    private Set<ProjectBack> mainProject;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "mainProject")
    private Set<ProjectBack> slaveProject;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "projects")
    private Set<Audit> audits;


    public Set<ProjectIncome> getProjectIncomeSet() {
        if(projectIncomeSet == null) {
            projectIncomeSet = new HashSet<ProjectIncome>();
        }
        return projectIncomeSet;
    }

    public void setProjectIncomeSet(Set<ProjectIncome> projectIncomeSet) {
        this.projectIncomeSet = projectIncomeSet;
    }


    public Set<Skus> getSkusSet() {
        if(skusSet == null) {
            skusSet = new HashSet<Skus>();
        }
        return skusSet;
    }

    public void setSkusSet(Set<Skus> skusSet) {
        this.skusSet = skusSet;
    }

    public Set<ProjectIntroduce> getProjectIntroduceSet() {
        if(projectIntroduceSet == null) {
            projectIntroduceSet = new HashSet<ProjectIntroduce>();
        }
        return projectIntroduceSet;
    }

    public void setProjectIntroduceSet(Set<ProjectIntroduce> projectIntroduceSet) {
        this.projectIntroduceSet = projectIntroduceSet;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Model getModel() {
        return model;
    }

    public void setModel(Model model) {
        this.model = model;
    }

    public Set<Agreement> getAgreement() {
        if(agreement == null) {
            agreement = new HashSet<Agreement>();
        }
        return agreement;
    }

    public void setAgreement(Set<Agreement> agreement) {
        this.agreement = agreement;
    }

    public Set<ProjectBack> getMainProject() {
        if(mainProject == null) {
            mainProject = new HashSet<ProjectBack>();
        }
        return mainProject;
    }

    public void setMainProject(Set<ProjectBack> mainProject) {
        this.mainProject = mainProject;
    }

    public Set<ProjectBack> getSlaveProject() {
        if(slaveProject == null) {
            slaveProject = new HashSet<ProjectBack>();
        }
        return slaveProject;
    }

    public void setSlaveProject(Set<ProjectBack> slaveProject) {
        this.slaveProject = slaveProject;
    }

    public Set<Audit> getAudits() {
        if(audits == null) {
            audits = new HashSet<Audit>();
        }
        return audits;
    }

    public void setAudits(Set<Audit> audits) {
        this.audits = audits;
    }

    public Long getProjectId() {
        return projectId;
    }

    public void setProjectId(Long projectId) {
        if(projectId == null) {
            this.projectId = 0L;
        } else {
            this.projectId = projectId;
        }
    }
}
